//
// Created by DELL on 2022/4/19.
//

#include "AppBase.h"
#include "render/Context.h"
#include "render/Device.h"
#include "render/SwapChain.h"
#include "GLWindow.h"
#include "TimeSys.h"

USING_NS_VULKAN

AppBase::AppBase(int width, int height, const std::string& title)
{
    this->window = GLWindow::create(width, height, title);

}

void AppBase::run()
{
    this->initContext();
    this->device = Device::create();
    this->swapChain = SwapChain::create(device, window);
    EventSystem::get()->dispatchCustomEvent(CustomEvent::initSystemEvent);
    this->init();

    while (!this->window->shouldClose())
    {
        TimeSys::update();
        this->window->update();
        EventSystem::get()->update();

        this->drawFrame();
    }
    vkDeviceWaitIdle(this->device->getDevice());

    EventSystem::get()->dispatchCustomEvent(CustomEvent::exitSystemEvent);

    this->cleanup();
    swapChain.reset();
    device.reset();
    Context::get()->unInitialize();
    this->window.reset();
}

void AppBase::initContext()
{
    Context::get()->initialize(this->window->getHwnd());
}
